package com.licheedev.someext.recycler;

import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.NotNull;

/**
 * 只封装了点击事件的Adapter）
 */

public abstract class BindingRecyclerAdapter<T, VB extends ViewBinding>

    extends RecyclerView.Adapter<BindingViewHolder<VB>> {

    private OnItemClickListener<VB> mOnItemClickListener;
    private OnItemLongClickListener<VB> mOnItemLongClickListener;
    protected final ArrayList<T> mData = new ArrayList<>();

    public interface OnItemClickListener<VB> {
        void onItemClick(ViewGroup parent, VB viewBinding, int position);
    }

    public interface OnItemLongClickListener<VB> {
        boolean onItemLongClick(ViewGroup parent, VB viewBinding, int position);
    }

    public BindingRecyclerAdapter() {
    }

    /**
     * @param enableHasStableIds true,启用 setHasStableIds(true)，让{@link #notifyDataSetChanged()}刷新数据时不闪烁
     */
    public BindingRecyclerAdapter(boolean enableHasStableIds) {
        mEnableHasStableIds = enableHasStableIds;
        setHasStableIds(enableHasStableIds);
    }

    private boolean mEnableHasStableIds = false;

    /**
     * 启用 setHasStableIds(true)，让{@link #notifyDataSetChanged()}刷新数据时不闪烁
     */
    public void enableHasStableIds() {
        mEnableHasStableIds = true;
        setHasStableIds(true);
    }

    @Override
    public long getItemId(int position) {
        if (mEnableHasStableIds) {
            return position;
        }
        return super.getItemId(position);
    }

    @NotNull
    @Override
    public BindingViewHolder<VB> onCreateViewHolder(final ViewGroup parent, int viewType) {

        final BindingViewHolder<VB> holder = createViewHolderInstance(parent, viewType);

        View view = holder.itemView;

        view.setOnClickListener(newOnItemClickListener(parent, holder));

        view.setOnLongClickListener(newOnItemLongClickListener(parent, holder));

        setChildViewListener(parent, holder, viewType);

        return holder;
    }

    /**
     * 创建单击条目监听
     *
     * @param parent
     * @param holder
     * @return
     */
    @NotNull
    protected View.OnClickListener newOnItemClickListener(final ViewGroup parent,
        final BindingViewHolder<VB> holder) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                if (mOnItemClickListener != null && position != RecyclerView.NO_POSITION) {
                    mOnItemClickListener.onItemClick(parent, holder.getBinding(), position);
                }
            }
        };
    }

    /**
     * 创建长按条目监听
     *
     * @param parent
     * @param holder
     * @return
     */
    @NotNull
    protected View.OnLongClickListener newOnItemLongClickListener(final ViewGroup parent,
        final BindingViewHolder<VB> holder) {
        return new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                int position = holder.getAdapterPosition();
                if (mOnItemLongClickListener != null && position != RecyclerView.NO_POSITION) {
                    return mOnItemLongClickListener.onItemLongClick(parent, holder.getBinding(),
                        position);
                } else {
                    return false;
                }
            }
        };
    }

    /**
     * 创建ViewHolder实例
     *
     * @param parent recyclerView
     * @param viewType itemView的类型
     * @return
     */
    @NonNull
    public abstract BindingViewHolder<VB> createViewHolderInstance(@NonNull ViewGroup parent,
        int viewType);

    /**
     * 设置子控件的监听事件，可以通过{@link RecyclerView.ViewHolder#getAdapterPosition()}拿到item的位置
     *
     * @param parent
     * @param holder
     * @param viewType
     */
    public void setChildViewListener(@NonNull ViewGroup parent,
        @NonNull BindingViewHolder<VB> holder, int viewType) {
        // 默认空实现
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public @NonNull
    List<T> getData() {
        return mData;
    }

    /**
     * 删除数据
     *
     * @param position
     */
    public void delete(int position) {
        mData.remove(position);
        notifyDataSetChanged();
    }

    /**
     * 获取item
     *
     * @param position
     * @return
     */
    public T getItem(int position) {
        return mData.get(position);
    }

    /**
     * 设置新数据
     *
     * @param data
     */
    public void setNewData(List<? extends T> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    /**
     * 追加数据
     *
     * @param data
     */
    public void appendData(List<? extends T> data) {
        if (data != null && data.size() > 0) {
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加单个数据
     *
     * @param data
     */
    public void addData(T data) {
        if (data != null) {
            mData.add(data);
        }
        notifyDataSetChanged();
    }

    /**
     * 设置点击事件
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener<VB> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    /**
     * 设置长按事件
     *
     * @param onItemLongClickListener
     */
    public void setOnItemLongClickListener(OnItemLongClickListener<VB> onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }
}

